﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace natureskitchen
{

    public partial class frmExpensesSummary : Form
    {

        public frmExpensesSummary()
        {
            InitializeComponent();
        }

        public bool checkAllBlank()
        {
            if (txtAdvertising.Text == string.Empty & txtFood.Text == string.Empty & txtLabor.Text == string.Empty & txtRent.Text == string.Empty & txtUtilities.Text == string.Empty)
            {
                return true;
            }
            return false;
        }

        public void deleteColumn(string columnName)
        {
            try
            {
                mysql.connectDB();
                string deleteQuery = "ALTER TABLE Expenses DROP " + columnName;
                MySql.Data.MySqlClient.MySqlCommand deleteCommand = new MySql.Data.MySqlClient.MySqlCommand(deleteQuery, mysql.connectDB());
                deleteCommand.ExecuteNonQuery();
                mysql.disconnectDB();
            }
            catch (MySql.Data.MySqlClient.MySqlException mysqlEX)
            {
                MessageBox.Show("There was an error while trying to delete that info from the database: " + mysqlEX.Message);
            }
        }

        private void btnEditRent_Click(object sender, EventArgs e)
        {
            txtRent.ReadOnly = false;
            btnUpdate.Enabled = true;
        }

        private void btnEditFood_Click(object sender, EventArgs e)
        {
            txtFood.ReadOnly = false;
            btnUpdate.Enabled = true;
        }

        private void btnEditLabor_Click(object sender, EventArgs e)
        {
            txtLabor.ReadOnly = false;
            btnUpdate.Enabled = true;
        }

        private void btnEditUtilities_Click(object sender, EventArgs e)
        {
            txtUtilities.ReadOnly = false;
            btnUpdate.Enabled = true;
        }

        private void btnEditAdvertising_Click(object sender, EventArgs e)
        {
            txtAdvertising.ReadOnly = false;
            btnUpdate.Enabled = true;
        }

        private void btnNewExpense_Click(object sender, EventArgs e)
        {
            frmNewExpense newexpense = new frmNewExpense();
            newexpense.Owner = this;
            newexpense.Show();
        }

        private void frmExpensesSummary_Load(object sender, EventArgs e)
        {
            try
            {
                mysql.connectDB();
                DataSet ds = new DataSet("ExpensesDataSet");
                string sqexpense = "SELECT Advertising, Food, Labor, Rent, Utilities FROM Expenses";
                MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(sqexpense, mysql.connectDB());
                da.Fill(ds);
                txtAdvertising.DataBindings.Add("Text", ds.Tables[0], "Advertising");
                txtFood.DataBindings.Add("Text", ds.Tables[0], "Food");
                txtLabor.DataBindings.Add("Text", ds.Tables[0], "Labor");
                txtRent.DataBindings.Add("Text", ds.Tables[0], "Rent");
                txtUtilities.DataBindings.Add("Text", ds.Tables[0], "Utilities");
                mysql.disconnectDB();
            }
            catch (ArgumentException)
            {
                txtAdvertising.Text = "";
                txtAdvertising.ReadOnly = false;
                txtFood.Text = "";
                txtFood.ReadOnly = false;
                txtLabor.Text = "";
                txtLabor.ReadOnly = false;
                txtRent.Text = "";
                txtRent.ReadOnly = false;
                txtUtilities.Text = "";
                txtUtilities.ReadOnly = false;
                btnAdd.Visible = true;
                btnUpdate.Visible = false;
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                MessageBox.Show("There was an error while connecting to the database");
            }
            if (checkAllBlank())
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtAdvertising.Text == string.Empty)
            {
                MessageBox.Show("Please enter in an amount for advertising");
            }
            if (txtFood.Text == string.Empty)
            {
                MessageBox.Show("Please enter in an amount for food");
            }
            if (txtLabor.Text == string.Empty)
            {
                MessageBox.Show("Please enter in an amount for labor");
            }
            if (txtRent.Text == string.Empty)
            {
                MessageBox.Show("Please enter in an amount for rent");
            }
            if (txtUtilities.Text == string.Empty)
            {
                MessageBox.Show("Please enter in an amount for utilities");
            }
            else
            {
                try
                {
                    mysql.connectDB().Open();
                    string updateQuery = "";
                    MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(updateQuery, mysql.connectDB());
                    command.ExecuteNonQuery();
                    MessageBox.Show("The expenses have been updated");
                    mysql.disconnectDB();
                }
                catch (MySql.Data.MySqlClient.MySqlException mysqlEx)
                {
                    MessageBox.Show("There was an error while connecting to the database:" + mysqlEx.Message);
                }
                catch (InvalidOperationException ioe)
                {
                    MessageBox.Show("There was an error while connecting to the database:" + ioe.Message);
                }
                txtAdvertising.ReadOnly = true;
                txtFood.ReadOnly = true;
                txtLabor.ReadOnly = true;
                txtRent.ReadOnly = true;
                txtUtilities.ReadOnly = true;
            }
        }

        private void btnDeleteRent_Click(object sender, EventArgs e)
        {
            frmDeleteVerification dv = new frmDeleteVerification(lblRent.Text);
            dv.Show();
            txtRent.Text = "";
        }

        private void btnDeleteUtilities_Click(object sender, EventArgs e)
        {
            frmDeleteVerification dv = new frmDeleteVerification(lblUtilities.Text);
            dv.Show();
            txtUtilities.Text = "";
        }

        private void btnDeleteLabor_Click(object sender, EventArgs e)
        {
            frmDeleteVerification dv = new frmDeleteVerification(lblLabor.Text);
            dv.Show();
            txtLabor.Text = "";
        }

        private void btnDeleteFood_Click(object sender, EventArgs e)
        {
            frmDeleteVerification dv = new frmDeleteVerification(lblFood.Text);
            dv.Show();
            txtFood.Text = "";
        }

        private void btnDeleteAdvertising_Click(object sender, EventArgs e)
        {
            frmDeleteVerification dv = new frmDeleteVerification(lblAdvertising.Text);
            dv.Show();
            txtAdvertising.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                mysql.connectDB().Open();
                string insertquery = "INSERT INTO Expenses (Advertising, Food, Rent, Utilities, Labor, Date) VALUES (";
                insertquery += "\'" + txtAdvertising.Text + "\'" + ", ";
                insertquery += "\'" + txtFood.Text + "\'" + ", ";
                insertquery += "\'" + txtRent.Text + "\'" + ", ";
                insertquery += "\'" + txtUtilities.Text + "\'" + ", ";
                insertquery += "\'" + txtLabor.Text + "\'" + ", ";
                insertquery += "\'" + DateTime.Now.ToString("yyyy-MM-dd") + "\'" + ") ";
                MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(insertquery, mysql.connectDB());
                command.ExecuteNonQuery();
                MessageBox.Show("The expenses have been added into the database");
                mysql.disconnectDB();
            }
            catch (MySql.Data.MySqlClient.MySqlException)
            {
                MessageBox.Show("There was an error while connecting to the database");
            }
        }
    }
}
